package com.xxx.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xxx.blog.entity.UserBook;
import com.xxx.blog.mapper.UserBookMapper;
import com.xxx.blog.service.UserBookService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author WangXJ
 * @since 2021-08-18
 */
@Service
public class UserBookServiceImpl extends ServiceImpl<UserBookMapper, UserBook> implements UserBookService {

    @Autowired
    UserBookMapper userBookMapper;

    @Override
    public int insertOneInCar(int uid, int bid, int bsum) {
        QueryWrapper<UserBook> wrapper1 = new QueryWrapper<>();
        wrapper1.eq("uid",uid);
        wrapper1.eq("bid",bid);
        if (userBookMapper.selectOne(wrapper1) != null) {
            UserBook userBook = userBookMapper.selectOne(wrapper1);
            UpdateWrapper<UserBook> wrapper2 = new UpdateWrapper<>();
            wrapper2.eq("uid",uid);
            wrapper2.eq("bid",bid);
            UserBook userBook1 = new UserBook();
            userBook1.setBsum(userBook.getBsum()+bsum);
            userBookMapper.update(userBook1,wrapper2);
            return 2;
        }else{
            UserBook ub = new UserBook();
            ub.setUid(uid);
            ub.setBid(bid);
            ub.setBsum(bsum);
            return userBookMapper.insert(ub);
        }

    }

    @Override
    public List<UserBook> findCartByUid(int uid) {
        QueryWrapper<UserBook> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        return userBookMapper.selectList(wrapper);
    }

    @Override
    public int deleteOne(int uid,int bid) {
        QueryWrapper<UserBook> wrapper = new QueryWrapper<>();
        wrapper.eq("uid",uid);
        wrapper.eq("bid",bid);
        return userBookMapper.delete(wrapper);
    }
}
